<template>
  <div class="index b">
      <!-- 登录状态 -->
      <div class="user">
        <span v-show="u" class="user-p">未登录</span> 
        <span v-show="!u" class="user-p logout" @click="logout">欢迎:{{user.uname}}</span> 
      </div>
      <!-- 播放页面 -->
      <div class="play-block" id="play-block">
        <!-- 图片封面 -->
        <div class="play-song-img">
          <img class="rotate-img b" :src="getPlaySongImg(playSong.img)" alt="">
        </div>
        <!-- 歌词 -->
        <div class="lyric" v-html="playSong.lyric"></div>
        <!-- 歌名 -->
        <div class="play-song-name">{{playSong.name}}</div>
        <div class="play-song-singer">{{playSong.singer}}</div>
      </div>
      <div class="show-block">
        <!-- 左侧分类 -->
        <div class="left-seletion">
          <div class="seletion b" v-for="type,index in seletions" :key="index" @click="queryByType(type)">{{type}}</div>
        </div>
        <!-- 搜索框 -->
        <div class="song-query b">
          <input type="text" placeholder="根据歌名搜索" class="song-query-input" v-model="inputStr" @keyup.enter="queryByKeyword(inputStr)">
        </div>
        <div class="song-query-enter b" @click="queryByKeyword(inputStr)">搜索</div>
        <!-- 主页图标 -->
        <div class="home-logo" @click="toIndex">
          <img src="../assets/image/Home.png" alt="" class="img-home" >
          <span class="text-home">HOME</span>
        </div>
        <!-- 登录与后台 -->
        <div class="link">
          <img src="../assets/image/Login.png" alt="" class="img-login"  @click="toLogin">
          <img src="../assets/image/Setting.png" alt="" class="img-setting" @click="toSetting">
        </div>
        <!-- 曲目列表 -->
        <div class="song-list b">
          <table class="song-list-table">
            <thead>
              <td class="thead-song-name">歌名({{listLength}})</td>
              <td class="thead-song-singer">歌手</td>
              <td class="thead-song-play-time">播放次数</td>
              <td class="thead-song-creation-time">收录时间</td>
              <td class="thead-song-love"></td>
            </thead>
            <template v-for="song,index in songlist">
              <tr :key="index">
                <td class="td-song-name" @click="setSongInPlayer(song.musicUrlID, song.mid)">{{song.musicName}}</td>
                <td class="td-song-singer">{{song.musicSinger}}</td>
                <td class="td-song-play-time">{{song.musicPlayTimes}}&nbsp;次</td>
                <td class="td-song-creation-time">{{song.musicCreationTime}}</td>
                <td class="td-song-love" @click="addOrDeleteFavourite(song.mid)">❤♡</td>
              </tr>
              <tr :key="index"><td colspan="5"><div class="line b"></div></td></tr>
            </template>
          </table>
        </div>
      </div>
      <!-- 播放器 -->
      <div class="song-player b">
        <iframe frameborder="no" border="0" marginwidth="0" marginheight="0" class="iframe163"
          :src="set163id(playerID)"></iframe>
        <!-- 弹出播放页面 -->
        <div class="open-play-block" @click="openPlayBlock(mid)">︽</div>
      </div>
  </div>
</template>

<script>
import {mapState} from 'vuex'
export default {
  data:function(){
    return{
      songlist:[],
      mid:'',
      seletions:["所有歌曲","我的喜欢❤","说唱 ☊","流行 ♪"],
      inputStr:"",
      playerID:0,
      playBlock:false,
      playSong:{
        name:'',
        singer:'',
        lyric:'',
        musicImg:''
      },
      playImgBase:'http://localhost:8098/MusicWebSpringboot/image/'
    }
  },
  computed:{
    ...mapState(['user']),
    listLength:function(){
        return this.songlist.length
    },
    u:function() {
      return this.user.uname === undefined
    }
  },
  methods:{
    // 关键字查询歌曲
    queryByKeyword:function(){
      this.$axios.get("/api/querySongByKeyword",{
        params:{
          keyword:this.inputStr
        }
      }).then((response)=>{
        this.songlist = response.data
      })
    },
    // 左侧栏歌曲分类
    queryByType:function(type){
      if (type === this.seletions[0]) {
        this.$axios.get("/api/queryAllSongs").then((response)=>{
          this.songlist = response.data
        })
      } else if (type === this.seletions[1]) {
        if (this.user.uname === undefined) {
          this.$router.push('/login')
          return
        }
        this.$axios.get("/api/querySongByUname", {
          params:{
            uname:this.user.uname
          }
        }).then((response)=>{
          this.songlist = response.data
        })
      } else {
        type = type.substring(0, 2)
        console.log(type);
        this.$axios.get("/api/querySongByType", {
          params:{
            type:type
          }
        }).then((response)=>{
          this.songlist = response.data
        })
      }
    },
    // 设置当前播放器歌曲
    setSongInPlayer:function(musicUrlID, mid) {
      this.playerID = musicUrlID
      this.addPlayTimes(mid)
      this.mid = mid
    },
    // 增加播放次数
    addPlayTimes:function(mid) {
      this.$axios({
        url:'/api/addPlayTimesByMid',
        method:"post",
        params:{
          mid:mid
        }
      }).then((response)=>{
          this.songlist = response.data
      })
    },
    set163id:function(id){
      return "//music.163.com/outchain/player?type=2&id=" + id + "&auto=1&height=100"
    },
    // 收藏或取消收藏（我的喜欢）
    addOrDeleteFavourite:function(mid) {
      if (this.user.uname === undefined) {
        this.$router.push('/login')
        return
      }
      this.$axios({
        url:'/api/addOrDeleteSongFromFavouriteListByUname',
        method:"get",
        params:{
          uname:this.user.uname,
          mid:mid
        }
      }).then((response)=>{
          if (response.data === "SuccessAdd") {
            this.$notify({
              title: '❤',
              message: '加入了收藏'
            });
          } else {
            this.$notify({
              title: '♡',
              message: '取消了收藏'
            });
          }
      })
    },
    // 打开播放页面
    openPlayBlock:function(mid){
      this.$axios.get('/api/querySongByMid', {
        params:{
          mid:mid
        }
      }).then((response)=>{
        console.log(response.data);
        this.playSong.lyric = response.data.musicLyric.replaceAll(" ", '</br>').replaceAll("1", " ")
        this.playSong.img = response.data.musicImg
        this.playSong.name = response.data.musicName
        this.playSong.singer = response.data.musicSinger
        console.log(this.playSong.img);
        if (this.playBlock) {
          document.getElementById("play-block").style.cssText = "top:1000px;"
          this.playBlock = false
        } else {
          document.getElementById("play-block").style.cssText = "top:0;"
          this.playBlock = true
        }
      })
    },
    getPlaySongImg:function(img) {
      var path = this.playImgBase + img
      console.log(path);
      return path
    },
    // 转去主页
    toIndex:function() {
      this.$router.push('/index')
    },
    // 转去后台
    toSetting:function(){
      if (this.user.uname === undefined) {
        this.$router.push('/login')
        return
      }
      this.$router.push('/setting')
    },
    // 转去登录
    toLogin:function(){
      this.$router.push('/login')
    },
    // 注销
    logout:function(){
      this.$confirm('要注销了吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.user.uname = undefined
          this.$message({
            type: 'info',
            message: '用户已注销'
          });
        })
    }
  },
  created:function(){
    // 所有曲目
    this.$axios.get("/api/queryAllSongs").then((response)=>{
      this.songlist = response.data
    })
  }
}
</script>

<style scoped>
.b{
  border: 2px black solid;
}
.index{
  width: 100%;
  min-height:98vh;
  position: relative;
  overflow: hidden;
}
.play-block{
  position:absolute;
  top: 1100px;
  width: 100%;
  height: 830px;
  background: #fff;
  border-top: 2px solid black;
  transition: ease-in-out 0.5s;
  z-index: 100;
}

.rotate-img{
  position:absolute;
  top: 8%;
  left: 21%;
  margin: 50px auto 50px;
  width: 400px;
  height: 400px;
  border-radius: 50%;
  animation: rotateImg 15s linear infinite;
  box-shadow: 4px 4px 4px 4px #55525285;
  overflow: hidden;
  transition: all 0.5s ease-in-out 0.1s;
}
@keyframes rotateImg{
 0%{
 transform: scale(0.8) rotate(0deg);
 box-shadow: 4px 4px 4px 4px rgba(158, 106, 243, 0.64);
 }
 25%{
 transform: scale(0.9) rotate(90deg);
 box-shadow: 4px 4px 4px 4px rgba(158, 106, 243, 0.64);
 }
50%{
 transform: scale(1) rotate(180deg);
box-shadow: 0 2px 2px 0 rgba(158, 106, 243, 0.64);
 }
75%{
 transform: scale(0.9) rotate(270deg);
 box-shadow: 4px 4px 4px 4px rgba(158, 106, 243, 0.64);
 }
 100%{
 transform: scale(0.8) rotate(360deg);
 box-shadow: 4px 4px 4px 4px rgba(158, 106, 243, 0.64);
 }

}
.lyric{
  position:absolute;
  top: 100px;
  left: 50%;
  width: 30%;
  height: 650px;
  font-size: 20px;
  letter-spacing: 5px;
  white-space: pre-wrap;
  overflow-y:scroll;
  background: #fff;
  border-left: #000 2px solid;
  padding-left: 100px;
  /* font-weight:bold;  */
}
.play-song-name{
  position: relative;
  left: 18%;
  top: 68%;
  width: 500px;
  height: 80px;
  font-size: 60px;
  /* font-weight: bold; */
  text-align: center;
  line-height: 100px;
  font-family: "华文楷体";
}
.play-song-singer{
  position: relative;
  left: 21%;
  top: 70%;
  width: 400px;
  height: 30px;
  font-size: 28px;
  text-align: center;
  line-height: 30px;
  /* font-weight: bold; */
  font-family: "华文楷体";
}

/* 定义滚动条样式 */
::-webkit-scrollbar {
    width: 4px;
    height: 6px;
    background-color: rgba(240, 240, 240, 0);
  }
    
  /*定义滚动条轨道 内阴影+圆角*/
  ::-webkit-scrollbar-track {
    box-shadow: inset 0 0 0px rgba(240, 240, 240, 0);
    border-radius: 10px;
    background-color: rgba(240, 240, 240, 0);
  }
    
  /*定义滑块 内阴影+圆角*/
  ::-webkit-scrollbar-thumb {
    border-radius: 10px;
    box-shadow: inset 0 0 0px rgba(240, 240, 240, 0);
    background-color: rgba(255, 255, 255, 0);
  }
.show-block{
  z-index: 0;
}

.song-query{
  position: absolute;
  top: 60px;
  left: 380px;
  width: 400px;
  height: 35px;
}
.song-query-input{
  outline: none;
  border: none;
  position: relative;
  left: 10px;
  height: 32px;
  width: 380px;
  font-size: 20px;
}
.song-query-enter{
  position:absolute;
  top: 60px;
  left: 780px;
  width: 100px;
  height: 35px;
  background: #000;
  font-size: 20px;
  color: #fff;
  text-align: center;
  line-height: 35px;
  cursor: pointer;
}
.song-list{
  position: absolute;
  top: 130px;
  left: 350px;
  width: 73%;
  height: 650px;
  font-size: 21px;
}
.line{
  margin-top: 5px;
  margin-bottom: 5px;
  border-bottom: none;
}

.song-list-table{
  padding-left: 50px;
  padding-top: 15px;
}
.thead-song-name{
  width: 400px;
  padding-bottom: 20px;
}
.thead-song-singer{
  width: 200px;
  padding-bottom: 20px;
}
.thead-song-play-time{
  width: 200px;
  padding-bottom: 20px;
}
.thead-song-creation-time{
  width: 300px;
  padding-bottom: 20px;
}
.thead-song-love{
  width: 200px;
  padding-bottom: 20px;
}
.td-song-name{
  cursor: pointer;
}
.td-song-love{
  cursor: pointer;
}
.td-song-play-time{
  padding-left: 40px;
}
.td-song-love:hover{
  font-size: 22px;
}


.left-seletion{
  position: absolute;
  top: 130px;
  left: 50px;
  width: 250px;
  height: 600px;
}
.seletion{
  width: 100%;
  height: 50px;
  margin-bottom: 35px;
  text-align: center;
  line-height: 50px;
  font-size: 21px;
  cursor: pointer;
}
.seletion:hover{
  background: #3d3d3d;
  color: #fff;
  transition: ease-in-out 0.1s;
}
.song-player{
  position: absolute;
  bottom: 0;
  height: 150px;
  width: 100%;
  border-left: none;
  border-right: none;
  border-bottom: none;
  background: #fff;
  z-index: 200;
}
.open-play-block{
  position:absolute;
  top: 7px;
  left: 7px;
  width: 88px;
  height: 88px;
  font-size: 40px;
  text-align:center;
  line-height: 130px;
  color: rgba(0, 0, 0, 0.144);
  cursor: pointer;
}
.iframe163{
  width: 100%;
}
.link{
  position:absolute;
  left: 1750px;
  top: 20px;
  width: 5.5%;
  height: 50px;
}
.img-login{
  position:absolute;
  left: 0;
  width: 50px;
  cursor: pointer;
}
.img-setting{
  position:absolute;
  right: 0;
  top: 5px;
  width: 40px;
  cursor: pointer;
}
.home-logo{
  position:absolute;
  left: 20px;
  top: 15px;
  width: 5%;
  height: 40px;
  cursor: pointer;
}
.img-home{
  position:relative;
  left: 0;
  width: 35px;
}
.text-home{
  position:absolute;
  left: 45px;
  top: 10px;
  font-size: 15px;
}
.user{
  position: absolute;
  top: 3%;
  right: 8%;
  height: 30px;
  width: 120px;
  font-size: 20px;
}
.user-p{
  position:absolute;
  top: 0;
}
.logout{
  cursor: pointer;
}
</style>

